import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.SelectRequest;


public class SimpleDBQueries {
	
	public static void findPlagiats(String myDomain, String... keywords) throws IOException
	{
		AmazonSimpleDB sdb = new AmazonSimpleDBClient(new 
				PropertiesCredentials(SimpleDBQueries.class.getResourceAsStream("AwsCredentials.properties")));

		try
		{
			// Select data from a domain
			String selectExpression = "SELECT Pdf, ParagraphNo FROM `" + myDomain + "`";

			if (keywords.length > 0)
			{
				selectExpression += " where";
				for (String keyword : keywords)
					selectExpression += " Top10Words = '" + keyword + "' intersection";
				
				selectExpression = selectExpression.substring(0, selectExpression.lastIndexOf(" intersection"));
			}
			
			System.out.println("Selecting: " + selectExpression + "\n");
			SelectRequest selectRequest = new SelectRequest(selectExpression);
			
			String pdfName="";
			String paragraph="";
			for (Item item : sdb.select(selectRequest).getItems())
			{
				Attribute attribute = item.getAttributes().get(0);
				System.out.print(attribute.getName() + ": ");
				System.out.print(attribute.getValue() + " - ");
				if(attribute.getName().equals("Pdf"))
					pdfName = attribute.getValue();
				else
					paragraph = attribute.getValue();
				
				attribute = item.getAttributes().get(1);
				System.out.print(attribute.getName() + ": ");
				System.out.print(attribute.getValue());		
				if(attribute.getName().equals("Pdf"))
					pdfName = attribute.getValue();
				else
					paragraph = attribute.getValue();
					
				
				/* Access PDF paragraph */
				AmazonS3 s3pdf = new AmazonS3Client(
						new PropertiesCredentials(SimpleDBQueries .class
								.getResourceAsStream("AwsCredentials.properties")));
				
				System.out.println(pdfName+paragraph);
				S3Object object = s3pdf.getObject(new GetObjectRequest("pdfparagraphbucket",pdfName+paragraph));
				BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
				String line;
				while((line = reader.readLine()) != null)
				{
					System.out.println(line);
				}
				
				System.out.println("\n");
			}
			System.out.println();

			
			
		}
		catch (AmazonServiceException ase)
		{
			System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SimpleDB, but was rejected with an error response for some reason.");
			System.out.println("Error Message:    " + ase.getMessage());
			System.out.println("HTTP Status Code: " + ase.getStatusCode());
			System.out.println("AWS Error Code:   " + ase.getErrorCode());
			System.out.println("Error Type:       " + ase.getErrorType());
			System.out.println("Request ID:       " + ase.getRequestId());
		}
		catch (AmazonClientException ace)
		{
			System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with SimpleDB, " + "such as not being able to access the network.");
			System.out.println("Error Message: " + ace.getMessage());
		}
	}
}
